OB.Emes_ActionButton = OB.Emes_ActionButton || {};

//拉动计划派工
OB.Emes_ActionButton.JobOrderAssign = function(params, win) {
	var grid = win.view.viewGrid;
	var records = grid.getSelectedRecords();
	
	var data = {ids: [], data: []};
	for (var i = 0;i < records.length;i ++) {
		data.ids.push(records[i].id);
		//data.data.push(records[i]);
	}
	data.ids = data.ids.join(",");
	OB.Emes_ActionButton.AssignOrders = data;
    var params = {
    	width: '90%',
        height: '90%',
    	url: "web/com.mes/html/FZJpaigong.html"
	};
    OB.Emes_ActionButton.orderAssignOpenWindow = win;
	isc.EMES_Window_View.create(params).show();
	// OB.Emes_ActionButton.orderAssignWindow = isc.EMESO_Order_Assign_Facilites.create(data);
	// OB.Emes_ActionButton.orderAssignWindow.show();
};

/*
* 生产订单派工
*/
isc.defineClass("EMESO_Order_Assign_Facilites", isc.OBPopup).addProperties({
	title: "生产派工",
	width: '80%',
	height: '350px',
	align: 'center',
	showMinimizeButton : false,
	showMaximizeButton : false,
	defaultLayoutAlign: 'center',
	allItem : [],
	orderId: null,
	submitLoading: false,
	submit: function(isForce) {
        orderList.saveEdits();
		var description = this.editForm.getValue("description");
        var facId = this.editForm.getValue("facId");
        var upResult = orderList.data;
        var cL = upResult.length;
       // var nums= null;
        var selectQtys = [];
        if (!facId) {
            isc.showMessage("请选择飞织机!");
            return;
        }
        for (var i = 0; i < cL; i++) {
            var qtys = upResult[i].qtys;
            var oldqty = upResult[i].oldqty;
            var qty = upResult[i].qty;

            if(qtys == 0){
                isc.showMessage("第"+i+1+"行请输入派工数量!");
                return;
            }
            if((parseInt(oldqty)+parseInt(qtys) ) > qty ){
                isc.showMessage("第"+parseInt(i)+parseInt(1)+"行派工数量超过未生产数量!");
                return;
            }

           // nums  =parseInt(oldqty)+parseInt(qtys);


            selectQtys.push(qtys);
        }

        if (!description) {
            description = "";
		}

		if (this.submitLoading) {
			return;
		}
		var _self = this;
		isc.confirm("确定提交派工单？", function(bool){
			if (bool) {
				var data = {
					ids: _self.orderData.ids.join(","),
                    facId: facId,
                    description: description,
                    nums :selectQtys.join(","),
					type: "submit"
				};
				_self.submitLoading = true;
				OB.RemoteCallManager.call("com.mes.handler.plan.JobOrderAssignHandler", data, {}, function(response, data, request){
					_self.submitLoading = false;
					if (data.result) {
						_self.serchData();
						
						if (OB.Emes_ActionButton.orderAssignOpenWindow) {
							OB.Emes_ActionButton.orderAssignOpenWindow.refresh();
						}
                        _self.closeClick();
					}
					isc.showMessage(data.msg);
				});
			}
		});
	},
	initWidget: function (data) {
		this.orderData = data;

		var _self = this;
		//OB控件 追加	addField or addItem	会有样式和事件找不到的问题
		
		this.orderList = isc.ListGrid.create({
			ID: "orderList",
            width: '60%',
            height: '340px',
			fields: [
                {width: "*", type: 'text', canEdit: false, title: '生产订单编码', name: 'code', readOnlyDisplay: "static", startRow: false},
				{width: "*", type: 'text', canEdit: false, title: '形体编码', name: 'pCode', readOnlyDisplay: "static", startRow: false},
				{width: "*", type: 'text', canEdit: false, title: '形体名称', name: 'pName', readOnlyDisplay: "static", startRow: false},
				{width: "*", type: 'text', canEdit: false, title: '生产数量', name: 'qty', readOnlyDisplay: "static", startRow: false},
                {width: "*", type: 'text', canEdit: false, title: '已派工数量', name: 'oldqty', readOnlyDisplay: "static", startRow: false},
                {width: "*", type: 'text', canEdit: true, title: '派工数量', name: 'qtys', readOnlyDisplay: "static", startRow: false},
                {width: "*", type: 'text', canEdit: false, title: '尺码', name: 'size', readOnlyDisplay: "static", startRow: false}
			]
		});
		
		this.initEditForm();

		this.items = [
			isc.HLayout.create({
				width: '100%',
				height: '100%',
				layoutMargin : 0,
				membersMargin : 0,
				members : [
					this.orderList,
					this.editFormPlanel
				]
			})
		];
		this.Super("initWidget", arguments);
		this.serchData();
	},
	initEditForm: function() {
        _self = this;
        this.editForm = isc.DynamicForm.create({
            ID: "editForm",
            numCols: 2,
            height: '100px',
            fields: [
                {width: "*", type: 'select', canEdit: true, startRow: true, name: 'facId',valueMap: {}, colSpan: 2,
                    title: "<div style='font-size: 13px;float: left;text-align: right;width: 90%;color: #ff0000;'>飞织机</div>"},
                {width: "*",  type: 'textarea', canEdit: true, startRow: true, name: 'description', colSpan: 2,height: 60,
                    title: "<div style='font-size: 13px;float: left;text-align: right;width: 90%;color: #0055ff;'>备注</div>"}
            ]
        });

        this.submitBtn = isc.OBFormButton.create({
            ID: 'submitBtn',
            title : '提交',
            action : function() {
                _self.submit();
            }
        });
        this.closeBtn = isc.OBFormButton.create({
            ID: 'closeBtn',
            title : '关闭',
            action : function() {
                _self.closeClick();
            }
        });

        this.editFormPlanel = isc.VLayout.create({
            width: '39%',
            height: '80%',
            layoutMargin: 15,
            membersMargin: 10,
            members: [
                this.editForm,
                isc.HLayout.create({
                    height: 150,
                    layoutMargin : 20,
                    membersMargin : 20,
                    align: 'center',
                    defaultLayoutAlign: 'center',
                    members : [
                        this.submitBtn,
                        this.closeBtn
                    ]
                })
            ]
        })
	},
	serchData: function() {
		var _self = this;
		OB.RemoteCallManager.call("com.mes.handler.plan.JobOrderAssignHandler", {ids: _self.orderData.ids.join(","), type: "getFacList"}, {},
		function(response, data, request) {
			if (!data.result) {
				isc.showMessage(data.msg, isc.OBMessageBar.TYPE_INFO);
				return;
			}
			if(data.facList) {
				var map　=　{};
				for (var i = 0;i < data.facList.length;i ++) {
                    map[data.facList[i].id] = data.facList[i].code + ' - ' + data.facList[i].name;
				}
                _self.editForm.getField("facId").setValueMap(map);
			}
            if(data.orderList) {
                _self.setGridValue(data.orderList, _self.orderList);
            }
		});
	},
	setValue: function(data, form){
		var itemIds = form.getFields();
		for(var i = 0 ; i < itemIds.length; i ++){
			var column = itemIds[i];
			this.setFieldValue(form.getField(column.name), data[column.name]);
		}
	},
	setFieldValue: function(field, value) {
		if (field.readOnlyDisplay == "static") {
			field.setReadOnlyDisplay("");
			field.setValue(value);
			field.setReadOnlyDisplay("static");
		} else {
			field.setValue(value);
		}
		field.show();
	},
	setGridValue: function(data, grid){
		var rows = grid.data;
		for (var i = 0;i < rows.length;i ++) {
			grid.removeData(rows[i]);
		}
		grid.setData(data);
	},
	clearValue: function(form) {
		var itemIds = form.getFields();
		for(var i = 0 ; i < itemIds.length; i ++){
			var name = itemIds[i];
			if(form.getField(name)){
				form.clearValue(name);
			}
		}
	}
});
